package com.youhome.clients.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2017/10/16.
 */
@RestController
@RequestMapping(value = "/news")
@Api(tags = "新闻")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @ApiOperation(value = "新闻列表", notes = "新闻列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "type", value = "新闻类别", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", required = true, paramType = "path")
    })
    @GetMapping(value = "/list/{type}/{pageNumber}")
    public Object list(@PathVariable Integer type, @PathVariable Integer pageNumber) throws CustomException {
        Integer pageSize = 10;
        return new ResultPage<>(ResultCode.SUCCESS, newsService.selectByType(type, pageNumber, pageSize));
    }

    @ApiOperation(value = "Top10新闻列表", notes = "Top10新闻列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/top")
    public Object top() throws CustomException {
        return new ResultPage<>(ResultCode.SUCCESS, newsService.selectTop10());
    }

    @ApiOperation(value = "新闻详情", notes = "新闻详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "id", value = "新闻ID", required = true, paramType = "path"),
    })
    @GetMapping(value = "/detail/{id}")
    public Object detail(@PathVariable Long id) throws CustomException {
        return new Result(ResultCode.SUCCESS, newsService.selectByPrimaryKey(id));
    }
}
